<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button
            type="primary"
            size="small"
            @click="handleClick('', 1)"
          >添加权限</el-button>
        </div>
        <el-table v-loading="loading" border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-button
                v-if="scope.row.type === 1"
                type="text"
                @click="handleClick(scope.row.id, 2)"
              >添加</el-button>
              <el-button
                type="text"
                @click="bian(scope.row.id)"
              >编辑</el-button>
              <el-button
                type="text"
                @click="shanchu(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 弹框 -->
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
      >
        <!-- 表单内容 -->
        <el-form ref="so" label-width="100px">
          <el-form-item label="权限名称">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="que">确 定</el-button>
        </span>
      </el-dialog>
      <!--  -->
    </div>
  </div>
</template>
<script>
import {
  getPermissionList,
  delPermission,
  addPermission,
  getPermissionDetail,
  updatePermission
} from '@/api/quanxian'
import { tranListToTreeData } from '@/utils/index'
export default {
  name: 'VueAdminTemplateIndex',

  data() {
    return {
      list: [],
      loading: false,
      dialogVisible: false,
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: 1 // 类型
      },
      sow: false
    }
  },
  created() {
    this.getList()
  },
  mounted() {},

  methods: {
    // 获取数据
    async getList() {
      this.loading = true
      const res = await getPermissionList()
      console.log(res)
      const re = await tranListToTreeData(res)
      this.list = re
      this.loading = false
    },
    // 删除权限
    async shanchu(id) {
      const res = await delPermission(id)
      console.log(res)
      this.getList()
    },
    // 添加
    handleClick(type, id) {
      this.sow = false
      this.dialogVisible = true
      this.formData.pid = type
      this.formData.type = id
    },
    // 关闭回调
    handleClose() {
      this.dialogVisible = false
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: 1 // 类型
      }
    },
    // 确认
    async que() {
      console.log(this.formData)
      if (this.sow) {
        await this.bianji()
      } else {
        const res = await addPermission(this.formData)
        console.log(res)
      }

      this.handleClose()
      this.getList()
    },
    // 编辑获取数据
    async bian(id) {
      this.sow = true

      this.formData = await getPermissionDetail(id)
      this.dialogVisible = true
    },
    // 编辑确认
    async bianji() {
      const res = await updatePermission(this.formData)
      console.log(res)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
